script(type='text/javascript/)
	$(document).ready(function() {
		$('#PortType')[0].setSelectedOption('!{port.type}');
		$('#PortDirection')[0].setSelectedOption('!{port.direction}');
		if(!{port.direction == 'Input'})
			$('#PortSmoothing')[0].setSelectedOption('!{port.smoothing}');
	});
	
	var newRow = function(id, name) {
		return '<tr><td class="neutral">inputs.' + name
			+ '<input type="button" value="Remove" onclick="removeInput(this)"/>'
			+ '<input type="hidden" name="inputs[]" value="' + id + '" />'
			+ '</td></tr>';
	};
	
	addInput = function() {
		var rows = $('#inputsHead').children('td:first').attr('rowspan');
		$(newRow($("#inputSelect option:selected")[0].value,
			$("#inputSelect option:selected")[0].innerHTML)).insertAfter("#inputsHead");
		$('#inputsHead').children('td:first').attr('rowspan', rows*1 + 1);
	};
	
	removeInput = function(self) {
		var rows = $('#inputsHead').children('td:first').attr('rowspan');
		$(self).parent().parent().remove();
		$('#inputsHead').children('td:first').attr('rowspan', rows*1 - 1);
	};
form(
	method='post',
	action='/port',
	enctype='application/x-www-form-urlencoded')
	table
		tr
			td.neutral(colspan='2')
				h3= title
				input#save(type='submit',value='Save')
				input(type='hidden',name='_id',value= port._id)
				input(type='hidden',name='device',value= port.device)
				input(type='hidden',name='action',value= action)
		-if(action == 'update')
			tr
				td.neutral.editLabel Device
				td.neutral.link(onclick='window.location.href=\'/device/' + port.device._id + '\'')= port.device.name
		tr
			td.neutral.editLabel Name
			td.neutral
				input(type='text',name='name',value= port.name)		
		tr
			td.neutral.editLabel System ID
			td.neutral
				input(type='text',name='id',value= port.sysId)
		tr
			td.neutral.editLabel Type
			td.neutral
				select#PortType(name='type')
					option(value='Boolean') Boolean
					option(value='Number') Number
		tr
			td.neutral.editLabel Direction
			td.neutral
				select#PortDirection(name='direction')
					option(value='Input') Input
					option(value='Output') Output
		-if(port.direction == 'Input')
			tr
				td.neutral.editLabel Sample Smoothing
					input(type='hidden',name='inputs[]',value='')
					input(type='hidden',name='relation',value=' ')
				td.neutral
					select#PortSmoothing(name='smooth')
						option(value='0') None
						option(value='2') 2
						option(value='3') 3
						option(value='4') 4
						option(value='5') 5
						option(value='6') 6
						option(value='7') 7
						option(value='8') 8
						option(value='9') 9
						option(value='10') 10
		-else if(port.direction == 'Output')
			tr#inputsHead
				td(rowspan= port.inputs.length + 1).neutral Inputs
					input(type='hidden',name='smooth',value='0')
				td.neutral
					select#inputSelect(style='width: 72%')
						each input in inputs
							-if(input._id + '' != port._id + '')
								option(value= input._id)= input.sysId
					input#editAddInput(
						type='button',
						value='Add Port',
						onclick='addInput()').link
			each input in port.inputs
				tr
					td.neutral
						.link(onclick='window.location=\'/port/' + input._id + '\'')= 'inputs.' + input.sysId
							-if(input.direction == 'Input')
								span= ' ==> ' + input.value
							-else if(input.direction == 'Output')
								span= ' <== ' + input.value
						input(type='button',value='Remove').link(onclick='removeInput(this)')
						input(type='hidden',name='inputs[]',value= input._id)
			tr
				td.neutral.editLabel Relation
				td.neutral
					b function(inputs) {
					div(style='margin-left: 32px;')
						textarea(name='relation', rows='16', cols='48')= port.relation
						span return output;
					b }
		tr
			td.neutral.editLabel Description
			td.neutral
				input(type='text',name='description',value= port.description)
